/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bbmangadownloader.gui;

import bbmangadownloader.gui.model.HistoryTableModel;
import bbmangadownloader.manager.HistoryManager;
import bbmangadownloader.manager.entity.History;
import bbmangadownloader.ult.MySwingUtilities;
import gasproject.utilities.AccentRemover;
import javax.swing.RowFilter;

/**
 *
 * @author Bach
 */
public class HistoryManagerDialog extends javax.swing.JDialog {

    private final HistoryTableModel tblModel;

    /**
     * Creates new form HistoryManagerDialog
     *
     * @param parent
     * @param modal
     */
    public HistoryManagerDialog(MangaDownloadGUI parent, boolean modal) {
        super(parent, modal);
        initComponents();

        tblModel = new HistoryTableModel();
        tblBookMark.setModel(tblModel);
        tblBookMark.setComponentPopupMenu(popSelect);
        tblModel.setEditable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popSelect = new javax.swing.JPopupMenu();
        mnSelect = new javax.swing.JMenuItem();
        mnDelete = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        txtSearch = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblBookMark = new org.jdesktop.swingx.JXTable();
        chkEdit = new javax.swing.JCheckBox();

        mnSelect.setText("Fletch");
        mnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnSelectActionPerformed(evt);
            }
        });
        popSelect.add(mnSelect);

        mnDelete.setText("Delete");
        mnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnDeleteActionPerformed(evt);
            }
        });
        popSelect.add(mnDelete);

        setTitle("History");

        jLabel1.setText("Search keyword");

        txtSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtSearchKeyReleased(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("History List"));

        tblBookMark.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Title 1"
            }
        ));
        tblBookMark.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblBookMarkMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tblBookMark);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 263, Short.MAX_VALUE)
        );

        chkEdit.setText("Editable?");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 71, Short.MAX_VALUE)
                .addComponent(chkEdit)
                .addContainerGap())
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkEdit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblBookMarkMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblBookMarkMouseReleased
        if (!chkEdit.isSelected() && MySwingUtilities.isDoubleLeftClick(evt)) {
            actionCheck();
        }
    }//GEN-LAST:event_tblBookMarkMouseReleased

    private void txtSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSearchKeyReleased
        final String text = AccentRemover.removeAccent(txtSearch.getText()).toLowerCase();

        tblBookMark.setRowFilter(new RowFilter<HistoryTableModel, Integer>() {

            @Override
            public boolean include(RowFilter.Entry<? extends HistoryTableModel, ? extends Integer> entry) {
                for (int i = 0; i < entry.getValueCount(); i++) {
                    if (AccentRemover.removeAccent(entry.getValue(i).toString()).toLowerCase()
                            .contains(text)) {
                        return true;
                    }
                }
                return false;
            }
        });
    }//GEN-LAST:event_txtSearchKeyReleased

    private void mnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnSelectActionPerformed
        actionCheck();
    }//GEN-LAST:event_mnSelectActionPerformed

    private void mnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnDeleteActionPerformed
        actionDelete();
    }//GEN-LAST:event_mnDeleteActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox chkEdit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem mnDelete;
    private javax.swing.JMenuItem mnSelect;
    private javax.swing.JPopupMenu popSelect;
    private org.jdesktop.swingx.JXTable tblBookMark;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration//GEN-END:variables

    private void actionCheck() {
        History m = MySwingUtilities.getSelectedObject(tblBookMark);
        if (m != null) {
            ((MangaDownloadGUI) getParent()).setCheckByHistory(m);
        }
    }

    private void actionDelete() {
        History m = MySwingUtilities.getSelectedObject(tblBookMark);
        if (m != null) {
            HistoryManager.removeHistory(m);
            tblModel.fireTableDataChanged();
        }
    }

    public void reload() {
        tblModel.fireTableDataChanged();
    }
}
